package com.maoatao.easycodegen.core;

import com.baomidou.mybatisplus.annotation.IdType;
import lombok.Getter;
import lombok.Setter;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.HashSet;

/**
 * 代码生成器配置类
 *
 * @author MaoAtao
 * @date 2025-10-04
 */
@Getter
@Setter
public class GeneratorConfig {
    /**
     * web 层输出路径
     */
    private String webOutputPath = "src/test";
    /**
     * model 层输出路径
     */
    private String modelOutputPath = "src/test";
    /**
     * service 层输出路径
     */
    private String serviceOutputPath = "src/test";
    /**
     * dao 层输出路径
     */
    private String daoOutputPath = "src/test";

    /**
     * 表前缀
     */
    private String tablePrefix = "t_";
    /**
     * 包名
     */
    private String packageName = "com.example.test";
    /**
     * 分类名 可为空
     */
    private String categoryName;
    /**
     * 模块名 可为空
     */
    private String moduleName;
    /**
     * 作者名
     */
    private String authorName = "CodeGeneratorTest";
    /**
     * url 风格
     */
    private UrlStyle urlStyle = UrlStyle.HYPHENATED;

    /**
     * 主键生成策略
     */
    private IdType idType = IdType.ASSIGN_ID;

    /**
     * 字段类型映射配置
     */
    private Map<String, String> fieldTypeMapping = new HashMap<>();

    /**
     * ddl
     */
    private String ddl;

    /**
     * 模板类型集合
     */
    private Set<Template> templates = new HashSet<>();

    public GeneratorConfig() {
        initDefaultTypeMapping();
    }

    public void putFieldTypeMapping(String dbType, String javaType) {
        fieldTypeMapping.put(dbType, javaType);
    }

    public void addTemplates(Template... templates) {
        this.templates.addAll(List.of(templates));
    }

    public void addTemplates(Collection<Template> templates) {
        this.templates.addAll(templates);
    }

    private void initDefaultTypeMapping() {
        fieldTypeMapping.put("tinyint", "Boolean");
        fieldTypeMapping.put("smallint", "Integer");
        fieldTypeMapping.put("mediumint", "Integer");
        fieldTypeMapping.put("int", "Integer");
        fieldTypeMapping.put("integer", "Integer");
        fieldTypeMapping.put("bigint", "Long");
        fieldTypeMapping.put("float", "Float");
        fieldTypeMapping.put("double", "Double");
        fieldTypeMapping.put("decimal", "BigDecimal");
        fieldTypeMapping.put("bit", "Boolean");
        fieldTypeMapping.put("char", "String");
        fieldTypeMapping.put("varchar", "String");
        fieldTypeMapping.put("tinytext", "String");
        fieldTypeMapping.put("text", "String");
        fieldTypeMapping.put("mediumtext", "String");
        fieldTypeMapping.put("longtext", "String");
        fieldTypeMapping.put("date", "LocalDateTime");
        fieldTypeMapping.put("datetime", "LocalDateTime");
        fieldTypeMapping.put("timestamp", "LocalDateTime");
    }
}
